package com.oocl.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.dao.GoodsDao;
import com.oocl.dao.WareHouseDao;
import com.oocl.dao.impl.GoodsDaoImpl;
import com.oocl.dao.impl.WareHouseDaoImpl;
import com.oocl.pojo.Goods;

/**
 * Servlet implementation class AddGoodsServlet
 */
public class AddGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private GoodsDao goodsDao;
	private WareHouseDao wareHouseDao;
	
    public AddGoodsServlet() {
        super();
        goodsDao = new GoodsDaoImpl();
        wareHouseDao = new WareHouseDaoImpl();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().setAttribute("uid", 1);
		String wareHouse = request.getParameter("wareHouse");
		String goodName = request.getParameter("name");
		double price = Double.parseDouble(request.getParameter("price"));
		int count = Integer.parseInt(request.getParameter("count"));
		String cpd = request.getParameter("pd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date pd = null;
		try {
			pd = sdf.parse(cpd);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Goods good = new Goods(UUID.randomUUID().toString(), goodName, price, count, pd, wareHouseDao.findIdByAddr(wareHouse));
		goodsDao.addGoods(good);
		
		request.getRequestDispatcher("view").forward(request, response);
	}

}
